import datetime

from sqlalchemy.ext.declarative import declarative_base
from sonyflake import SonyFlake
from sqlalchemy import Column, BigInteger, DateTime, Integer, String, JSON, Float

# Base类: 创建每个数据库模型或类（ORM 模型）
Base = declarative_base()


class RequiredColumns(object):
    id = Column(BigInteger,
                primary_key=True,
                nullable=False,
                unique=True,
                default=lambda: SonyFlake().next_id())
    deleted = Column(Integer, nullable=False, default=0)
    created_date = Column(DateTime, nullable=False, default=lambda: datetime.datetime.now())
    updated_date = Column(DateTime, nullable=False, default=lambda: datetime.datetime.now(), onupdate=lambda: datetime.datetime.now())


class Message(Base, RequiredColumns):
    __tablename__ = "message"

    content = Column(String(300), nullable=False)


class Order(Base, RequiredColumns):
    __tablename__ = "order"

    status = Column(Integer, nullable=False)
    money = Column(Integer, nullable=False)  # 单位: 分


class Resource(Base, RequiredColumns):
    __tablename__ = "resource"

    url = Column(String(2000), nullable=False)  # 浏览器访问链接
    filetype = Column(Integer, nullable=False)  # 文件类型 -> mapping.py: filetype_mapping
    uri = Column(String(2000))  # 资源链接 -> 下载资源的直接链接
    title = Column(String(200))  # 标题
    deep = Column(Integer, default=0)  # 链接深度，当前页面深度为 0
    parent_url = Column(String(2000))  # 父级链接
    summary = Column(String(3000))  # 简介
    keywords = Column(String(1000))  # 关键词
    partition_ = Column(String(100))  # 分区（合集）
    views = Column(Integer, default=0)  # 观看人数
    video_id = Column(String(300))  # 视频 id
    publish_date = Column(DateTime)  # 上传日期
    author = Column(String(60))  # 作者
    filename = Column(String(200))  # 文件名
    size = Column(Float, default=0)  # 资源大小 -> 不同类型资源单位不一样 -> mapping.py: filetype_mapping
    status = Column(Integer, default=0)  # 状态 -> mapping.py: status_mapping
    extends = Column(JSON, default={})  # 扩展字段
